﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Xml;
using System.IO;
using System.Xml.Linq;
using AphroditePiano.Model;
using AphroditePiano.VO;
using AphroditePiano.Controls;

namespace AphroditePiano
{
    public partial class MainPage : UserControl
    {

        AutoPlayManager autopaly = new AutoPlayManager();

        public MainPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.KeyDown += new KeyEventHandler(KeyBoard_KeyDown);
            this.KeyUp += new KeyEventHandler(KeyBoard_KeyUp);
            PlaySpeedSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(PlaySpeedSlider_ValueChanged);
            SoundVolumeSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(SoundVolumeSlider_ValueChanged);
            WebClient clientData = new WebClient();
            clientData.DownloadStringAsync(new Uri(HtmlPage.Document.DocumentUri, "Music/Music.xml"));
            clientData.DownloadStringCompleted += new DownloadStringCompletedEventHandler(clientData_DownloadStringCompleted);
        }

        void SoundVolumeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            soundManager.SoundVolume = e.NewValue;
        }

        void PlaySpeedSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            autopaly.PlaySpeed = e.NewValue;
            xIndication.PlaySpeed = e.NewValue;
        }

        #region KeyBoard_KeyUp & KeyBoard_KeyDown
        void KeyBoard_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.A:
                    SLKeyBoardModel.Instance.IsADown = false;
                    break;
                case Key.B:
                    SLKeyBoardModel.Instance.IsBDown = false;
                    break;
                case Key.C:
                    SLKeyBoardModel.Instance.IsCDown = false;
                    break;
                case Key.D:
                    SLKeyBoardModel.Instance.IsDDown = false;
                    break;
                case Key.E:
                    SLKeyBoardModel.Instance.IsEDown = false;
                    break;
                case Key.F:
                    SLKeyBoardModel.Instance.IsFDown = false;
                    break;
                case Key.G:
                    SLKeyBoardModel.Instance.IsGDown = false;
                    break;
                case Key.H:
                    SLKeyBoardModel.Instance.IsHDown = false;
                    break;
                case Key.I:
                    SLKeyBoardModel.Instance.IsIDown = false;
                    break;
                case Key.J:
                    SLKeyBoardModel.Instance.IsJDown = false;
                    break;
                case Key.K:
                    SLKeyBoardModel.Instance.IsKDown = false;
                    break;
                case Key.L:
                    SLKeyBoardModel.Instance.IsLDown = false;
                    break;
                case Key.M:
                    SLKeyBoardModel.Instance.IsMDown = false;
                    break;
                case Key.N:
                    SLKeyBoardModel.Instance.IsNDown = false;
                    break;
                case Key.O:
                    SLKeyBoardModel.Instance.IsODown = false;
                    break;
                case Key.P:
                    SLKeyBoardModel.Instance.IsPDown = false;
                    break;
                case Key.Q:
                    SLKeyBoardModel.Instance.IsQDown = false;
                    break;
                case Key.R:
                    SLKeyBoardModel.Instance.IsRDown = false;
                    break;
                case Key.S:
                    SLKeyBoardModel.Instance.IsSDown = false;
                    break;
                case Key.T:
                    SLKeyBoardModel.Instance.IsTDown = false;
                    break;
                case Key.U:
                    SLKeyBoardModel.Instance.IsUDown = false;
                    break;
                case Key.V:
                    SLKeyBoardModel.Instance.IsVDown = false;
                    break;
                case Key.W:
                    SLKeyBoardModel.Instance.IsWDown = false;
                    break;
                case Key.X:
                    SLKeyBoardModel.Instance.IsXDown = false;
                    break;
                case Key.Y:
                    SLKeyBoardModel.Instance.IsYDown = false;
                    break;
                case Key.Z:
                    SLKeyBoardModel.Instance.IsZDown = false;
                    break;
            }
        }

        void KeyBoard_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.A:
                    SLKeyBoardModel.Instance.IsADown = true;
                    break;
                case Key.B:
                    SLKeyBoardModel.Instance.IsBDown = true;
                    break;
                case Key.C:
                    SLKeyBoardModel.Instance.IsCDown = true;
                    break;
                case Key.D:
                    SLKeyBoardModel.Instance.IsDDown = true;
                    break;
                case Key.E:
                    SLKeyBoardModel.Instance.IsEDown = true;
                    break;
                case Key.F:
                    SLKeyBoardModel.Instance.IsFDown = true;
                    break;
                case Key.G:
                    SLKeyBoardModel.Instance.IsGDown = true;
                    break;
                case Key.H:
                    SLKeyBoardModel.Instance.IsHDown = true;
                    break;
                case Key.I:
                    SLKeyBoardModel.Instance.IsIDown = true;
                    break;
                case Key.J:
                    SLKeyBoardModel.Instance.IsJDown = true;
                    break;
                case Key.K:
                    SLKeyBoardModel.Instance.IsKDown = true;
                    break;
                case Key.L:
                    SLKeyBoardModel.Instance.IsLDown = true;
                    break;
                case Key.M:
                    SLKeyBoardModel.Instance.IsMDown = true;
                    break;
                case Key.N:
                    SLKeyBoardModel.Instance.IsNDown = true;
                    break;
                case Key.O:
                    SLKeyBoardModel.Instance.IsODown = true;
                    break;
                case Key.P:
                    SLKeyBoardModel.Instance.IsPDown = true;
                    break;
                case Key.Q:
                    SLKeyBoardModel.Instance.IsQDown = true;
                    break;
                case Key.R:
                    SLKeyBoardModel.Instance.IsRDown = true;
                    break;
                case Key.S:
                    SLKeyBoardModel.Instance.IsSDown = true;
                    break;
                case Key.T:
                    SLKeyBoardModel.Instance.IsTDown = true;
                    break;
                case Key.U:
                    SLKeyBoardModel.Instance.IsUDown = true;
                    break;
                case Key.V:
                    SLKeyBoardModel.Instance.IsVDown = true;
                    break;
                case Key.W:
                    SLKeyBoardModel.Instance.IsWDown = true;
                    break;
                case Key.X:
                    SLKeyBoardModel.Instance.IsXDown = true;
                    break;
                case Key.Y:
                    SLKeyBoardModel.Instance.IsYDown = true;
                    break;
                case Key.Z:
                    SLKeyBoardModel.Instance.IsZDown = true;
                    break;
            }
        }
        #endregion

        void clientData_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            XmlReader reader = XmlReader.Create(new StringReader(e.Result));
            XDocument document = XDocument.Load(reader);
            AphroditePianoModel.Instance.musicList = (from music in document.Descendants("music")
                                                      select new MusicVO
                                                      {
                                                          Title = music.Attribute("title").Value,
                                                          PassKey = music.Value,
                                                          Level = int.Parse(music.Attribute("level").Value)
                                                      }).ToList<MusicVO>();
            LayoutRoot.Children.Add(autopaly);
            xDataGrid.ItemsSource = AphroditePianoModel.Instance.musicList;
            if (AphroditePianoModel.Instance.musicList.Count > 0) {
                SetViewer(AphroditePianoModel.Instance.musicList[0]);
            }
            xDataGrid.SelectionChanged += new SelectionChangedEventHandler(xDataGrid_SelectionChanged);
            BtnPlay.MouseLeftButtonUp += new MouseButtonEventHandler(BtnPlay_MouseLeftButtonUp);
            BtnStop.MouseLeftButtonUp += new MouseButtonEventHandler(BtnStop_MouseLeftButtonUp);
        }

        void BtnStop_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            autopaly.StopMusic();
        }

        void BtnPlay_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (xDataGrid.SelectedItem != null) {
                autopaly.PlayMusic((xDataGrid.SelectedItem as MusicVO));
            }
        }

        void xDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (xDataGrid.SelectedItem != null)
            {
                SetViewer((xDataGrid.SelectedItem as MusicVO));
            }
        }

        private void SetViewer(MusicVO music)
        {
            xScrollViewer.ScrollToVerticalOffset(0);
            txtMusicTitle.Text = music.Title;
            txtLyric.Text = convert2Lyric(music.PassKey);
            xIndication.musicVO = music;
        }

        private string convert2Lyric(string keys)
        {
            string strLyric = "";
            keys = keys.Replace(" ", string.Empty);
            keys = keys.Replace("\n", string.Empty);
            keys = keys.Replace("\t", string.Empty);
            keys = keys.Replace("|", "\n");
            keys = keys.Replace(","," ");
            keys = keys.Replace("&", string.Empty);
            strLyric = keys;
            return strLyric;
        }
    }
}
